-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track callsite for observers & hooks #15607
Track callsite for observers & hooks #15607
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this strategy, but I really want to see benchmarks before we remove the feature flag on BundleInserter
.
I tried running the various Should this ends up having to sit behind a feature flag, would this be a new feature flag or the (possibly renamed) |
Personally, I'd rename For hooks, that's a bit harder. Ideally an optional |
I think that would be the best, will do. Can you add the rename to your meta issue? |
ce81b1a
to
c985e7f
Compare
3671ee0
to
7092848
Compare
ci fix new hooks in dependant crates ci
d47f8f7
to
bd68936
Compare
Your PR increases Bevy Minimum Supported Rust Version. Please update the |
Your PR increases Bevy Minimum Supported Rust Version. Please update the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work. Trying to tie failures directly to user actions rather than via engine internals in a stack trace is definitely worth pursuing. I do have a couple suggestions that should be tackled in a follow-up PR to address some of the ergonomics issues we're starting to collect with track_caller
. But as stated, these are follow-up concerns.
// | ||
// `on_add` will trigger when a component is inserted onto an entity without it | ||
.on_add(|mut world, entity, component_id| { | ||
.on_add(|mut world, entity, component_id, caller| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up PR: The signature for a hook is getting pretty unwieldy. The component_id
was already an infrequently used parameter (IMO), and I imagine users ignoring caller
(even if they shouldn't!). It might be worth refactoring entity
, component_id
, and caller
into a Context
struct. I've omitted world
from that context since it's the only one out of these 4 that a user will mutate, so keeping it separate will avoid some partial-mutable-borow pains.
#[cfg(feature = "track_location")] | ||
caller, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up PR: I think this pattern is pretty annoying to deal with. It's verbose and off-putting (IMO). I think we should investigate if obfuscating this behind a Context
type which internally contains this feature gate is easier to use without sacrificing performance.
As much as I'm a no_std
advocate, another alternative could be to use thread-local storage to manage this information without explicitly passing it as a function argument.
I'll add these to #16775 when I get a sec. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
entity, | ||
component_id, | ||
#[cfg(feature = "track_location")] | ||
Some(caller), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use MaybeLocation
instead of Option<&Location>
? That would make it clear that the value is always present when the feature flag is enabled, and is never present when it's disabled.
That could be discussed in a follow-up, though! Really, I'd like to use something like that everywhere so that we don't need cfg
s everywhere and I don't get CI failures because I forgot to build with the feature toggled. But I think that's more controversial so we shouldn't block this PR on that discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I plan to coalesce the entity, component id and caller arguments into a single struct in a followup PR as suggested by bushRAT, which will have the same effect :)
Objective
Fixes #14708
Also fixes some commands not updating tracked location.
Solution
ObserverTrigger
has a newcaller
field with thetrack_change_detection
feature;hooks take an additional caller parameter (which is
Some(…)
orNone
depending on the feature).Testing
See the new tests in
src/observer/mod.rs
Showcase
Observers now know from where they were triggered (if
track_change_detection
is enabled):Migration
Option<&'static Location>
argument